package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {
    @Autowired
    private IDestinationService destinationService;
//    @Autowired
//    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")DestinationQuery qo){
        //这里是springdata的分页
        Page<Destination> page = destinationService.query(qo);
        model.addAttribute("page", page);
        model.addAttribute("toasts",destinationService.getToasts(qo.getParentId()));
        return "destination/list";//这里其实除了最初的加载的页面之外其他的所有的操作都是异步请求完成的
    }

    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public Object getDestByDeep(int deep){
        List<Destination> list = destinationService.list();
        return list;
    }

    @RequestMapping("/saveOrUpdate")//保存或者修改
    @ResponseBody
    public Object saveOrUpdate(Destination destination){
        destinationService.saveOrUpdate(destination);
        return JsonResult.success();
    }

    @RequestMapping("/getDestByDestinationId")
    @ResponseBody
    public Object getDestByDestinationId(String rid){//获取目的地根据地区id
        return  destinationService.getDestByDestinationId(rid);
    }

    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(String id, int hot){//修改热度  传递id 和hot 然后修改热度
        destinationService.changeHotValue(id, hot);
        return  JsonResult.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){//删除
        destinationService.delete(id);
        return  JsonResult.success();
    }

}

